<template>
  <div>
    <!-- <h3> -->
    <to-back></to-back>
    <!-- </h3> -->
    <van-address-list
      class="con"
      v-model="chosenAddressId"
      :list="list"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
       @select="onSelect"
    />
    <van-empty v-if="list.length == 0" image="search" description="暂无地址" />
    <!-- <van-button class="confirm" color="linear-gradient(to right, #ff6034, #ee0a24)">
 确认
    </van-button>-->
  </div>
</template>

<script>
import { reqaddressList } from "../../http/http";
export default {
  data() {
    return {
      chosenAddressId: "1",
      list: [
        //   了解字段
        /* {
          id: '1',
          name: '张三',
          tel: '13000000000',
          address: '浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室',
          isDefault: true,
        },*/
      ]
    };
  },
  mounted() {
    //  3. 一进来就发请求
    reqaddressList({
      uid: JSON.parse(localStorage.getItem("user")).uid
    }).then(res => {
      // 3.2成功的回调
      if (res.data.code == 200) {
        let arr = res.data.list ? res.data.list : [];
        // 将获取到的字段--->赋值给框架
        //  后端取回来的数据和前端件展示的字段不一致，需要转换一下
        arr = arr.map(item => {
          return {
            id: item.id,
            name: item.username,
            tel: item.userphone,
            address: item.location + item.useraddress,
            isDefault: item.sttus == 1
          };
        });
        this.list = arr;
      }
    });
  },
  methods: {
    onAdd() {
      // 4.  点击了添加按钮，跳转到地址添加页面
      this.$router.push("/addressAdd");
    },
    // 编辑地址好子女增地址是t同一个页面
    onEdit(item, index) {
      // console.log(item,index);
      // 跳转前带上这条数据的id
      this.$router.push("/addressAdd?id=" + item.id);
    },
    onSelect(item){
      // console.log(item);//只需要id
      this.$router.replace('/order?addressId='+item.id)
    }
  }
};
</script>

<style scoped>
.con {
  margin-top: 0.8rem;
}
</style>